﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Flare.Pages.Hards
{
    public partial class Index : UserControl, IPubTypicalPage, IDisposable
    {
        public string name
        {
            get { return "Оборудование"; }
        }

        public static int? id_department { get; set; }
        public static int? id_room { get; set; }

        public static int? id_type { get; set; }
        public static int? id_mark { get; set; }
        public static int? id_model { get; set; }

        public Index()
        {
            InitializeComponent();
            sortedLastUP.IsChecked = Properties.Settings.Default.HardWareShowLastUp;
            //this.Loaded += (s, e) => {
            //    if (techList.SelectedIndex != -1) CreateFullMenu((int)techList.SelectedValue);
            //};
        }

        private void sortedLastUP_CheckedUnchecked(object sender, RoutedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["equipmentsDataSet"]).MethodParameters[1] = sortedLastUP.IsChecked;
            Properties.Settings.Default.HardWareShowLastUp = sortedLastUP.IsChecked.Value;
        }

        private void Department_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["general_roomsDataSet"]).MethodParameters[0] = (int?)Department.SelectedValue;
            ((ObjectDataProvider)this.Resources["equipmentsDataSet"]).MethodParameters[3] = (int?)Department.SelectedValue;
        }  

        private void Room_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["equipmentsDataSet"]).MethodParameters[4] = (int?)Room.SelectedValue;
        }      

        private void techType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["equipment_marksDataSet"]).MethodParameters[0] = (int?)techType.SelectedValue;
            ((ObjectDataProvider)this.Resources["equipmentsDataSet"]).MethodParameters[5] = (int?)techType.SelectedValue;
        }

        private void techMark_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["equipment_modelsDataSet"]).MethodParameters[0] = (int?)techMark.SelectedValue;
            ((ObjectDataProvider)this.Resources["equipmentsDataSet"]).MethodParameters[6] = (int?)techMark.SelectedValue;
        }

        private void techModel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((ObjectDataProvider)this.Resources["equipmentsDataSet"]).MethodParameters[7] = (int?)techModel.SelectedValue;
        }

        private void ClearFilter_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Department.SelectedIndex = -1;
            techType.SelectedIndex = -1;
        }

        //private void ListBoxItem_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        //{
        //    int eq_id = ((Flare.DB.Entity.EquipmentView)((sender as ListBoxItem).Content)).id_equipment;
        //    glob.kernel.ShowPage(typeof(Pages.Hards.Details), eq_id);
        //    techList.SelectedItem = null;
        //    e.Handled = true;
        //}

        //private void ListBoxItem_MouseRightButtonDown(object sender, RoutedEventArgs e)
        //{
        //    if (techList.SelectedIndex != -1 && (sender as ListBoxItem).IsSelected == true)
        //    {
        //        CreateStartMenu();
        //        techList.SelectedItem = null;
        //        e.Handled = true;
        //    }
        //    else
        //    {
        //        CreateFullMenu(((Flare.DB.Entity.EquipmentView)((sender as ListBoxItem).Content)).id_equipment);
        //    }
        //}

        public void CreateStartMenu()
        {
            var _insert = new FunctionalPanelItems().Insert(typeof(Pages.Hards.Create));
            object[] funcItems = new object[] { _insert };
            glob.kernel.CreateFunctionalPanel(funcItems);
        }

        //void CreateFullMenu(int selected_id)
        //{
        //    var _insert = new FunctionalPanelItems().Insert(typeof(Pages.Hards.Create));
        //    var _move = new FunctionalPanelItems().Move(typeof(Pages.Hards.Move),selected_id);
        //    var _edit = new FunctionalPanelItems().Edit(typeof(Pages.Hards.Edit),selected_id);
        //    var _delete = new FunctionalPanelItems().Delete(typeof(Pages.Hards.Delete),selected_id);
            
        //    object[] funcItems = new object[] { _insert,  _move, _edit, _delete };
        //    glob.kernel.CreateFunctionalPanel(funcItems);
        //}

        public void SetSearchString(string _content)
        {
            ((ObjectDataProvider)this.Resources["equipmentsDataSet"]).MethodParameters[0] = _content;
        }

        public void Dispose()
        {
            id_department = (int?)Department.SelectedValue;
            id_room = (int?)Room.SelectedValue;
            id_type = (int?)techType.SelectedValue;
            id_mark = (int?)techMark.SelectedValue;
            id_model = (int?)techModel.SelectedValue;
        }

        private void techList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            glob.kernel.ShowPage(typeof(Pages.Hards.Details), techList.SelectedValue);
        }
    }


}
